package online.sh.cn.fy.users.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.DES3Util;
import online.sh.cn.fy.common.utils.ResponseUtils;
import online.sh.cn.fy.common.utils.TokenUtils;
import online.sh.cn.fy.common.utils.WxUtils;

/**
 * 个人中心修改手机号
 */
@WebServlet("/changemessage")
public class ChangeMessageServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		WxUtils wx=new WxUtils();
		String mobile=request.getParameter("mobile");
		String coder=request.getParameter("usercode");
		String lockcode=request.getParameter("lockcode");
		String openid=CookieUtil.getCookieValueByName(request, "openid");
		String exmobile=CookieUtil.getCookieValueByName(request, "messageMobile");
		String excode=CookieUtil.getCookieValueByName(request, "messageCode");
		String exlockcode=request.getSession().getAttribute("lockcode").toString().toLowerCase();
		//判断验证码和发送手机号
		System.out.println(exmobile+"##########"+excode);
		System.out.println(mobile+"*********"+coder);
		System.out.println("新认证用户openid:"+openid);
		JSONObject json=new JSONObject();
		if(null!=openid && ""!=openid){
			if(null!=lockcode && ""!=lockcode && exlockcode.equals(lockcode.toLowerCase())){
				if (null!=exmobile && null!=excode) {
					try {
						openid=DES3Util.decrypt(openid);
						exmobile=DES3Util.decrypt(exmobile);
						excode=DES3Util.decrypt(excode);
					} catch (Exception e) {
						log(e.getMessage());
					}
					if (exmobile.equals(mobile) && excode.equals(coder)) {
						
						json.put("result", "0"); //验证成功 入库 
						ResponseUtils.renderJson(response, json.toString());
					}else{
						json.put("result", "1"); //预留手机号或者验证码错误
						ResponseUtils.renderJson(response, json.toString());
					}
				}else{
					json.put("result", "2");   //未进行短消息发送的验证
					ResponseUtils.renderJson(response, json.toString());
				}
			}else{
				json.put("result", "4");   //图形验证码不正确
				ResponseUtils.renderJson(response, json.toString());
			}
		}else{
			json.put("result", "3");   //openid未获取
			ResponseUtils.renderJson(response, json.toString());
		}
	}

}
